﻿using NopSolutions.NopCommerce.BusinessLogic.Infrastructure;
using NopSolutions.NopCommerce.BusinessLogic.Services;
using System;
using System.Collections.Specialized;
using System.Security.Permissions;
using System.Web;

namespace NopSolutions.NopCommerce.BusinessLogic.Providers
{
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    public class NopSiteMapProvider : XmlSiteMapProvider
    {
        private SiteMapNode _rootNode = null;
        //private bool initialized = false;

        public override SiteMapNode RootNode {
            get {
                return _rootNode;
            }
        }

        public override void Initialize(string name, NameValueCollection attributes) {
            base.Initialize(name, attributes);
        }

        public override SiteMapNode BuildSiteMap() {
            if (_rootNode != null) {
                return _rootNode;
            }

            lock (this) {

                _rootNode = new SiteMapNode(this, Guid.NewGuid().ToString(), HttpRuntime.AppDomainAppVirtualPath, LocalizationService.GetLocaleResourceString("Content.HomePage"));

            }

            return _rootNode;
        }

        protected override SiteMapNode GetRootNodeCore() {
            if (_rootNode != null)
                return _rootNode;

            return BuildSiteMap();
        }

        private ILocalizationService LocalizationService {
            get { return IoC.Resolve<ILocalizationService>(); }
        }

        private ICategoryService CategoryService {
            get { return IoC.Resolve<ICategoryService>(); }
        }
    }
}
